﻿using System;
using System.Linq;
using Microsoft.VisualStudio.TestTools.UnitTesting;
using SharePointUtilitiesForDevelopers.Action;

namespace SharePointUtilitiesForDevelopers.Tests.Action
{
    [TestClass]
    public class ActionManagerTest
    {
        [TestMethod]
        public void LoadActions_should_load_correctly()
        {
            var manager = new ActionManager(new UnitTestActionLoader());
            manager.LoadActions();

            var actions = manager.GetActions(ActionTarget.ListView);
            Assert.AreEqual(1, actions.Count());

            actions = manager.GetActions(ActionTarget.List);
            Assert.AreEqual(1, actions.Count());

            actions = manager.GetActions(ActionTarget.List | ActionTarget.ListView);
            Assert.AreEqual(1, actions.Count());

            actions = manager.GetActions(ActionTarget.Site);
            Assert.AreEqual(0, actions.Count());
        }

        [TestMethod]
        public void GetsActions_should_return_all_actions_when_target_is_all()
        {
            var manager = new ActionManager(new UnitTestActionLoader());
            manager.LoadActions();
            var actions = manager.GetActions(ActionTarget.All);
            Assert.AreEqual(2, actions.Count());
        }

        [TestMethod]
        public void GetsActions_should_return_no_actions_when_target_isnt_present()
        {
            var manager = new ActionManager(new UnitTestActionLoader());
            manager.ClearActions();
            var actions = manager.GetActions(ActionTarget.ListView);
            Assert.AreEqual(0, actions.Count());
        }
    }

    public class UnitTestActionLoader : IActionLoader
    {
        public void Load(IActionManager manager)
        {
            manager.AddAction(new UnitTestAction());
            manager.AddAction(new UnitTestAllAction());
        }
    }


    public class UnitTestAction : IAction
    {
        public void Execute(ActionContext actionContext)
        {
            Console.WriteLine("Execute");
        }
        
        public string Title
        {
            get { return "Unit Test Action."; }
        }

        public string Description
        {
            get { return "A test action."; }
        }

        public ActionTarget Target
        {
            get { return ActionTarget.List | ActionTarget.ListView; }
        }

    }

    public class UnitTestAllAction : IAction
    {
        public void Execute(ActionContext actionContext)
        {
            Console.WriteLine("Execute");
        }

        public string Title
        {
            get { return "Unit Test Action."; }
        }

        public string Description
        {
            get { return "A test action."; }
        }

        public ActionTarget Target
        {
            get { return ActionTarget.All; }
        }

    }
}
